package controller;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import javax.servlet.http.HttpSession;

import cameraConfig.Standort;
import user.UserBean;
import cameraConfig.KameraDAO;

public class AddUserController extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public void process ( HttpServletRequest request, HttpServletResponse response ) {
		ServletContext context;
		RequestDispatcher dispatcher;
		
		HttpSession session;
		
		context = getServletContext();
		session = request.getSession();
		
		String error = null;
						
		context = getServletContext();
			
		UserBean curUser = (UserBean) (session.getAttribute("user"));	
		
		if(curUser != null) {
			if(request.getParameterNames() != null)
	        {		
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				String confirmPassword = request.getParameter("confPassword");
				String fullname = request.getParameter("fullname");
				String location = request.getParameter("location");
				String strIsAdmin = request.getParameter("admin");
				String strIsInactive = request.getParameter("inactive");
				boolean isAdmin = false;
				boolean isInactive = false;
				
				if(strIsAdmin != null)
				{
					if(strIsAdmin.equals("1"))
					{
						isAdmin = true;
					}
				}
							
				if(strIsInactive != null)
				{
					if(strIsInactive.equals("1"))
					{
						isInactive = true;
					}
				}
					
				if(username != null && password != null && fullname != null && confirmPassword != null)
				{
					if(password.equals(confirmPassword))
					{
						UserBean user = new UserBean();
						user.addUser(username, password, fullname, location, isAdmin, isInactive);
					}
					else
					{
						error = "Password and confirmation does not match";
					}
				}
				else
				{
					KameraDAO camDoa = new KameraDAO();
					Vector<Standort> locations  = camDoa.getLocations();
					request.setAttribute("locations", locations);
				}
			}
			else 
			{
				error = "Es wurde keine Variablen gesetzt.";
			}	
		}
		else
		{
			error = "User session expired";
		}
		
		if (error != null && !error.isEmpty())
			request.setAttribute("errorMessage", error);
		
		dispatcher = context.getRequestDispatcher("/addUser.jsp");
				
		try {
			dispatcher.forward(request, response);
			//response.sendRedirect("/user.jsp");
		} 
		catch (ServletException e) {
			e.printStackTrace();
			
		} 
		catch (IOException e) {
			e.printStackTrace();
		}	

	}

	public void doGet ( HttpServletRequest request, HttpServletResponse response ) {
		process(request, response);
	}
	
	public void doPost ( HttpServletRequest request, HttpServletResponse response ) {
		process(request, response);
	}
}


